<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <link rel="stylesheet" href="./css/tocart.css">
    <script src="./js/promiseajax.js"></script>
    <script src="./js/cookie.js"></script>
</head>
<body>
    <div class="box top">
        <div class="title">
            <a href="./login.html">登录</a>
            <a href="./register.html">注册</a>
        </div>
        <div class="listtitle">
            <a href="./list.html">购物车</a>
        </div>

    </div>
    <div class="box2">
        <table border="1">
            <thead>
                <tr>
                    <td>商品名</td>
                    <td>商品价格</td>
                    <td>商品数量</td>
                    <!-- <td>价格</td> -->
                </tr>
            </thead>
            <tbody>

            </tbody>

            </ttr>

        </table>
    </div>

</body>
<script>
    var box = document.querySelector('.box')
    var title = document.querySelector('.title')
    var username = getCookie('username')
    if (username) {
        title.innerHTML = `
        欢迎<a href="usercenter.html">${username}<a/>登录!!
        <a href="javascript:;" onclick = loginout()>退出登录</a>
        `
    }
    function loginout() {
        removeCookie('username')
        title.innerHTML = `
        <a href="./login.html">登录</a>
        <a href="./register.html">注册</a>
        `
    }

    goodscart()
    function goodscart() {

        request({
            url: '/api/goods/goodscart/' + getCookie('userid'),
        }).then((res) => {
            // console.log(res);
            var data = res.data
            // console.log(data);
            var goodsid = []
            data.forEach(item => {
                goodsid.push(item.goodsid)
            });
            var goodsidobj = { ...goodsid }
            // console.log(goodsidobj);

            //数量
            var numgoods = []
            data.forEach(item => {
                numgoods.push(item.goodsnumber)
            });
            // console.log(numgoods);

            request({
                url: '/api/goods/cartgoods',
                data: goodsidobj
            }).then((re) => {
                console.log(re);
                var da = re.data
                console.log(da);
                da = Object.values(da)
                console.log(da);
                var html = ''
                if (da.length === 0) {
                    return
                }
                da.forEach(items => {
                    html += `
                        <tr>
                            <td>${items.title}</td>
                            <td>${items.price}</td>
                            
                        </tr>
                    `
                });
                document.querySelector('tbody').innerHTML = html

                var trs = document.querySelectorAll('tbody tr')
                for (let i = 0; i < trs.length; i++) {
                    var td = document.createElement('td')
                    td.innerHTML =
                        `
                            <td>${numgoods[i]}</td>
                        `
                    trs[i].appendChild(td)
                }
            })
        })
    }
</script>

</html>